unit Login;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, MAIN, StdCtrls, Setting, DB, ZAbstractRODataset, ZAbstractDataset,
  ZAbstractTable, ZDataset, ZConnection, cHash, ExtCtrls;
//   Data_master,IdHashMessageDigest, idHash;

type
  TLoginForm = class(TForm)
    ZConnection1: TZConnection;
    ZTable1: TZTable;
    Panel1: TPanel;
    warningpanel: TPanel;
    Label4: TLabel;
    hashtext: TEdit;
    closebtn: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label5: TLabel;
    Edit3: TEdit;
    Button3: TButton;
    ZQuery1: TZQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    function GetDiskSerialNumber(Disk: char): string;
    procedure closebtnClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  LoginForm: TLoginForm;

implementation

{$R *.dfm}

procedure TLoginForm.Button1Click(Sender: TObject);
var mainform : TMainForm;
var
  P:Pointer;
  kode:string;
  sql_update1, sql_update2, sql_update3, sql_update4, sql_update5:string;
begin
  if((Edit1.Text<>'') and (Edit2.Text<>''))then
  begin
      //ngapdet tanggal proses
      sql_update1 := 'UPDATE `proses_berjalan` SET  `TANGGAL_SELESAI` =  ''2012-12-17'' WHERE  `proses_berjalan`.`ID` =3 ';

      sql_update2 := 'UPDATE `proses_berjalan` SET  `TANGGAL_MULAI` =  ''2012-12-18'','
        +'`TANGGAL_SELESAI` =  ''2013-01-02'' WHERE  `proses_berjalan`.`ID` =4 ';

      sql_update3 := 'UPDATE `proses_berjalan` SET  `TANGGAL_MULAI` =  ''2013-01-03'' WHERE  `proses_berjalan`.`ID` =5 ';

      sql_update4 := 'UPDATE `proses_berjalan` SET  `TANGGAL_MULAI` =  ''2012-12-18'','
        +'`TANGGAL_SELESAI` =  ''2013-01-02'' WHERE  `proses_berjalan`.`ID` =6 ';

      sql_update5 := 'UPDATE `proses_berjalan` SET  `TANGGAL_MULAI` =  ''2013-01-29'','
        +'`TANGGAL_SELESAI` =  ''2013-01-29'' WHERE  `proses_berjalan`.`ID` =7';

      zquery1.Active:=false;
      zQuery1.SQL.Clear;
      zquery1.SQL.Text:=sql_update1;
      zquery1.ExecSQL;

      zquery1.Active:=false;
      zQuery1.SQL.Clear;
      zquery1.SQL.Text:=sql_update2;
      zquery1.ExecSQL;

      zquery1.Active:=false;
      zQuery1.SQL.Clear;
      zquery1.SQL.Text:=sql_update3;
      zquery1.ExecSQL;

      zquery1.Active:=false;
      zQuery1.SQL.Clear;
      zquery1.SQL.Text:=sql_update4;
      zquery1.ExecSQL;

      zquery1.Active:=false;
      zQuery1.SQL.Clear;
      zquery1.SQL.Text:=sql_update5;
      zquery1.ExecSQL;

      ztable1.active:=false;
      ztable1.tablename:='user';
      ztable1.filter:='USERNAME='+QuotedStr(Edit1.Text)+' AND PASSWORD='+QuotedStr(Edit2.Text);
      ztable1.filtered:=true;
      ztable1.active:=true;
      ztable1.first;
      if(ztable1.RecordCount>0)then
      begin
        //  showmessage(ztable1.FieldByName('KODE_KEC').AsString);
        ShowMessage('Login Berhasil!');
//        CloseModal;
//        Hide;
        release;
        kode:=ztable1.FieldByName('KODE_KABKOT').AsString;
        if(ztable1.FieldByName('KODE_KEC').AsString<>'000')then
        begin
          kode:=kode+ztable1.FieldByName('KODE_KEC').AsString;

        end;
        mainform := TMainForm.Create(nil, kode, ztable1.FieldByName('USERNAME').AsString, ztable1.FieldByName('ROLE').AsString);
        P := @Application.Mainform;
        Pointer(P^) := mainform;
        mainform.Caption:='';
        mainform.Visible:=true;
        //mainform.ShowModal;
        //Close;



      end
      else
      begin
        ShowMessage('Maaf data yang Anda masukkan tidak ditemukan');
      end;


  end
  else begin
    ShowMessage('Silakan isi form dengan lengkap');
  end;
end;

procedure TLoginForm.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TLoginForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  action:=caFree;
end;


function TLoginForm.GetDiskSerialNumber(Disk: char): string;
var
 VolumeSerialNumber,
 MaximumComponentLength,
 FileSystemFlags: DWORD;
 SerialNumber: string;
begin

 GetVolumeInformation(PChar(Disk + ':\'),
                      nil,
                      0,
                      @VolumeSerialNumber,
                      MaximumComponentLength,
                      FileSystemFlags,
                      nil,
                      0);
 SerialNumber := Format('%.4X-%.4X', [HiWord(VolumeSerialNumber), LoWord(VolumeSerialNumber)]);

 Result := SerialNumber;

end;
procedure TLoginForm.FormCreate(Sender: TObject);
var setting:TSetting_Form;
  mfile:textfile;
  read:string;
  ispass:boolean;
  disklabel, sha1, sha2, sha3,buffer, picek:String;
  idx: integer;
begin
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.Connected:=true;
  ztable1.Connection:=ZConnection1;
  zquery1.Connection:=ZConnection1;
  ispass:=false;
  {disklabel :=    (GetDiskSerialNumber('C'));
    sha1 := SHA1DigestToHex(CalcSHA1(disklabel));
    idx:=2;
    while(idx<=Length(sha1)) do
    begin
      buffer:=buffer + sha1[idx];
      if ((idx mod 8 = 0) AND (idx mod 40 <> 0)) then
        buffer:=buffer+'-';
      idx:= idx + 2;
    end;
    //buffer := sha1[1]+sha1[5]+'-'+sha1[9]+sha1[13]+'-'+sha1[17]+sha1[21]+'-'+sha1[25]+sha1[29];
    sha1:= copy(buffer,1,9);
    sha2 := SHA256DigestToHex(CalcHMAC_SHA256('inspira',sha1));
    sha3 := SHA256DigestToHex(CalcHMAC_SHA256('inspira',sha2));
    picek:=sha2+sha3;
    buffer := '';
    idx:=4;
    while(idx<=Length(picek)) do
    begin
      buffer:=buffer + picek[idx];
      if ((idx mod 16 = 0) AND (idx mod 128 <> 0)) then
        buffer:=buffer+'-';
      idx:= idx + 4;
    end;
    picek := copy(buffer,1,9);
    //showMessage(picek);
  if(FileExists('key.txt'))then
  begin

      AssignFile(mfile, 'key.txt');
      Reset(mfile);
      Readln(mfile, read);

    if(read = picek) then
     ispass:=true;
     CloseFile(mfile);

  end;
  if(ispass<>true)then
  begin
    warningpanel.Visible:=true;
    hashtext.text:=sha1;
    //showmessage('Anda belum terdaftar, untuk mendaftar hubungi admin dan serahkan nomor berikut: '+sha1);
    //close;
  end
   }

end;

procedure TLoginForm.closebtnClick(Sender: TObject);
begin
close;
end;

procedure TLoginForm.Button3Click(Sender: TObject);
var
  key : string;
  mfile : textFile;
begin
  key := Edit3.Text;
//  showMessage(key);
  AssignFile(mfile, 'key.txt');
  ReWrite(mfile);
  write (mfile,key);
  
  CloseFile(mfile);
  showMessage('Kunci telah disimpan, silakan tutup lalu nyalakan aplikasi kembali');
end;
procedure TLoginForm.Edit2KeyPress(Sender: TObject; var Key: Char);
var mainform : TMainForm;
var
  P:Pointer;
  kode:string;
  sql_update1, sql_update2, sql_update3, sql_update4, sql_update5 :string;
begin
  if Key = #13 then begin
  if((Edit1.Text<>'') and (Edit2.Text<>''))then
  begin
      //ngapdet tanggal proses
      sql_update1 := 'UPDATE `proses_berjalan` SET  `TANGGAL_SELESAI` =  ''2012-12-17'' WHERE  `proses_berjalan`.`ID` =3 ';

      sql_update2 := 'UPDATE `proses_berjalan` SET  `TANGGAL_MULAI` =  ''2012-12-18'','
        +'`TANGGAL_SELESAI` =  ''2013-01-02'' WHERE  `proses_berjalan`.`ID` =4 ';

      sql_update3 := 'UPDATE `proses_berjalan` SET  `TANGGAL_MULAI` =  ''2013-01-03'' WHERE  `proses_berjalan`.`ID` =5 ';

      sql_update4 := 'UPDATE `proses_berjalan` SET  `TANGGAL_MULAI` =  ''2012-12-18'','
        +'`TANGGAL_SELESAI` =  ''2013-01-02'' WHERE  `proses_berjalan`.`ID` =6 ';

      sql_update5 := 'UPDATE `proses_berjalan` SET  `TANGGAL_MULAI` =  ''2013-01-29'','
        +'`TANGGAL_SELESAI` =  ''2013-01-29'' WHERE  `proses_berjalan`.`ID` =7';

      zquery1.Active:=false;
      zQuery1.SQL.Clear;
      zquery1.SQL.Text:=sql_update1;
      zquery1.ExecSQL;

      zquery1.Active:=false;
      zQuery1.SQL.Clear;
      zquery1.SQL.Text:=sql_update2;
      zquery1.ExecSQL;

      zquery1.Active:=false;
      zQuery1.SQL.Clear;
      zquery1.SQL.Text:=sql_update3;
      zquery1.ExecSQL;

      zquery1.Active:=false;
      zQuery1.SQL.Clear;
      zquery1.SQL.Text:=sql_update4;
      zquery1.ExecSQL;

      zquery1.Active:=false;
      zQuery1.SQL.Clear;
      zquery1.SQL.Text:=sql_update5;
      zquery1.ExecSQL;

      ztable1.active:=false;
      ztable1.tablename:='user';
      ztable1.filter:='USERNAME='+QuotedStr(Edit1.Text)+' AND PASSWORD='+QuotedStr(Edit2.Text);
      ztable1.filtered:=true;
      ztable1.active:=true;
      ztable1.first;
      if(ztable1.RecordCount>0)then
      begin
        ShowMessage('Login Berhasil!');
//        CloseModal;
//        Hide;
        release;
        kode:=ztable1.FieldByName('KODE_KABKOT').AsString;
        if(ztable1.FieldByName('KODE_KEC').AsString<>'000')then
        begin
          kode:=kode+ztable1.FieldByName('KODE_KEC').AsString;
        end;
        //showmessage('hello');
        mainform := TMainForm.Create(Application, kode, ztable1.FieldByName('USERNAME').AsString, ztable1.FieldByName('ROLE').AsString);
        P := @Application.Mainform;
        Pointer(P^) := mainform;
        mainform.Caption:='APLIKASI PENYUSUNAN DATA PEMILIH JAWA BARAT';
        mainform.Visible:=true;
        //mainform.ShowModal;
        //Close;

      end
      else
      begin
        ShowMessage('Maaf data yang Anda masukkan tidak ditemukan');
      end;


  end
  else begin
    ShowMessage('Silakan isi form dengan lengkap');
  end;
  end;
end;

end.
